﻿/********************************************************
 * 类名： InventoryBLL.cs
 * 功能： 库存业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2014-01-22	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System.Collections.Generic;
using System.Linq;
using PSS.DAL.CommonManage;
using PSS.Entity.InitPurManage;
using PSS.Utility;

namespace PSS.BLL.CommonManage
{
    public class InventoryBLL
    {
        private readonly InventoryDAL _dal = new InventoryDAL();

        public string GetInventoryQtyById(InventoryQcEntity qcmodel)
        {
            var list1 = _dal.GetStorage(qcmodel);
            var linqStament = from p in list1
                              group p by new { p.StorageCode, p.StorageName }
                                  into g
                                  select
                                      new
                                          {
                                              StorageCode = g.Key.StorageCode,
                                              StorageName = g.Key.StorageName,
                                              InventoryQty = g.Sum(p => p.InventoryQty)
                                          };
            var list2 = _dal.GetInventoryQtyById(qcmodel);
            var item = new List<InventoryEntity>();
            var list3 = new List<InventoryEntity>();
            if (list1.Count > 0)
            {
                var stepSet = from p in list2 group p by p.StorageCode into g select g;
                foreach (var i in stepSet)
                {
                    var model = new InventoryEntity();
                    decimal inTotalQty = 0;
                    decimal outTotalQty = 0;
                    foreach (var j in list2.Where(j => i.Key == j.StorageCode))
                    {
                        model.StorageName = j.StorageName;
                        if (j.InventoryType == 1)
                        {
                            inTotalQty = inTotalQty + j.InventoryQty;
                        }
                        else
                        {
                            outTotalQty = outTotalQty + j.InventoryQty;
                        }
                    }
                    model.StorageCode = i.Key;
                    model.InventoryQty = inTotalQty - outTotalQty;
                    item.Add(model);
                }
                foreach (var i in linqStament)
                {
                    var model = new InventoryEntity();
                    model.StorageCode = i.StorageCode;
                    model.StorageName = i.StorageName;
                    decimal qty = item.Where(j => i.StorageCode == j.StorageCode).Aggregate(i.InventoryQty, (current, j) => current + j.InventoryQty);
                    model.InventoryQty = qty;
                    list3.Add(model);
                }
            }
            else
            {
                var stepSet = from p in list2 group p by p.StorageCode into g select g;
                foreach (var i in stepSet)
                {
                    var model = new InventoryEntity();
                    decimal inTotalQty = 0;
                    decimal outTotalQty = 0;
                    foreach (var j in list2.Where(j => i.Key == j.StorageCode))
                    {
                        model.StorageName = j.StorageName;
                        if (j.InventoryType == 1)
                        {
                            inTotalQty = inTotalQty + j.InventoryQty;
                        }
                        else
                        {
                            outTotalQty = outTotalQty + j.InventoryQty;
                        }
                    }
                    model.StorageCode = i.Key;
                    model.InventoryQty = inTotalQty - outTotalQty;
                    list3.Add(model);
                }
            }
            return JsonHelper.Encode(list3);
        }
    }
}
